package com.itheima.restkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.basic.BasicPojo;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.pojo.Brand;
import com.itheima.restkeeper.pojo.Store;
import com.itheima.restkeeper.mapper.StoreMapper;
import com.itheima.restkeeper.req.StoreVo;
import com.itheima.restkeeper.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description：门店信息账号 服务实现类
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Override
    public Page<Store> findStoreVoPage(StoreVo storeVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<Store> brandPage = new Page<>(pageNum,pageSize);
        //构建查询条件
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        //按门店名称查询
        if(!EmptyUtil.isNullOrEmpty(storeVo.getStoreName())){
          queryWrapper.like(Store::getStoreName, storeVo.getStoreName());
        }
        //按门店状态查询
        if(!EmptyUtil.isNullOrEmpty(storeVo.getEnableFlag())){
            queryWrapper.eq(Store::getEnableFlag,storeVo.getEnableFlag());
        }
        Page<Store> page = this.page(brandPage, queryWrapper);
        return page;
    }

    @Override
    public Store createStore(StoreVo storeVo) {
        //转换BrandVo为Brand
        Store store = new Store();
        BeanConv.toBean(storeVo,store);
        boolean flag = this.save(store);
        if(flag){
            return store;
        }
        return null;
    }

    @Override
    public Boolean updateStore(StoreVo storeVo) {
        //转换BrandVo为Brand
        Store store = BeanConv.toBean(storeVo, Store.class);
        return this.updateById(store);
    }

    @Override
    public Boolean deleteStore(String[] checkedIds) {
        //转换数组为集合
        List<String> strings = Arrays.asList(checkedIds);
        List<Long> longs = new ArrayList<>();
        for (String string : strings) {
            longs.add(Long.valueOf(string)); //用Long中的方法将String类型数据转成Long类型
        }
        //批量删除
        return this.removeByIds(longs);
    }

    @Override
    public List<Store> findStoreVoList() {
        //构建查询条件
        LambdaQueryWrapper<Store> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Store::getEnableFlag, SuperConstant.YES);
        //查询有效状态 ,因为是多个
        return   this.list(queryWrapper);
    }

}
